package cn.lzy.web.functionextensionmvc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyMVCconfig implements WebMvcConfigurer {

    // 添加视图管理
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 请求toLoginPage 映射路径或者login.html 页面都会自动映射到login.html 页面
        // http://localhost:8080/toLoginPage1
        registry.addViewController("/toLoginPage1").setViewName("login");
        // http://localhost:8080/toLoginPage
        registry.addViewController("/toLoginPage").setViewName("login");
        // http://localhost:8080/login.html
        registry.addViewController("/login.html").setViewName("login");
    }


    @Autowired
    private MyInterceptor myInterceptor;

    // 添加拦截器管理
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor)
                .addPathPatterns("/**")   // 拦截所有路径请求
                .excludePathPatterns("/login.html");  // 放行 /login.html 路径请求
    }
}

